﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nage.Algorithms.RandomGenerators
{
    /// <summary>
    /// Random number generator class based on System.Random
    /// </summary>
    public sealed class Rng : IRng
    {
        #region Singleton
        private static Rng _instance = null;
        private static System.Random _random = new System.Random();
        private static Object _instanceLocker = new Object();
        private Rng()
        {
        }

        /// <summary>
        /// Gets the instance of Random number generator class
        /// </summary>
        /// <returns></returns>
        public static Rng GetInstance()
        {
            if (_instance == null)
            {
                lock (_instanceLocker)
                {
                    if (_instance == null)
                    {
                        _instance = new Rng();
                    }
                }
            }
            return _instance;
        } 
        #endregion

        #region GetDouble
        /// <summary>
        /// Returns a random number between 0.0 and 1.0
        /// </summary>
        /// <returns></returns>
        public double GetDouble()
        {
            return _random.NextDouble();
        }

        /// <summary>
        /// Returns a random number between 0.0 and max
        /// </summary>
        /// <param name="max">maximal value</param>
        /// <returns></returns>
        public double GetDouble(double max)
        {
            return _random.NextDouble() * max;
        }

        /// <summary>
        /// Returns a random number between min and max
        /// </summary>
        /// <param name="min">minimal value</param>
        /// <param name="max">maximal value</param>
        /// <returns></returns>
        public double GetDouble(double min, double max)
        {
            return _random.NextDouble() * (max - min) + min;
        } 
        #endregion

        #region Draw
        /// <summary>
        /// Draws a value with selected chances
        /// </summary>
        /// <param name="chances">chances</param>
        /// <returns></returns>
        public Boolean Draw(double chances)
        {
            return (_random.NextDouble() < chances);
        }
        #endregion
    }
}
